package com.yunduansing.core.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yunduansing.core.commons.DbCommon;
import com.yunduansing.core.commons.DbCommon.Grid;
import com.yunduansing.core.dao.TemplateDao;
import com.yunduansing.core.dto.Template;
import com.yunduansing.core.model.TemplateModel;
import com.yunduansing.core.service.TemplateService;
import com.yunduansing.core.utils.ResultUtil;

@Service
public class TemplateServiceImpl implements TemplateService {
	@Autowired
	private TemplateDao dao;

	public List<Template> getButtonsByTemplateId(int id) {
		System.out.println(id);
		return dao.getButtonsByTemplateId(id);
	}

	public Object pagination(Grid grid,Map<String,Object> keys) {
		DbCommon.Pager pager=new DbCommon.Pager();
		pager.setIndex(grid.getIndex());
		grid.setIndex((grid.getIndex()-1)*grid.getSize());
		pager.setData(dao.pagination(grid,keys));
		pager.setRows(dao.pageCount(keys));
		return pager;
	}

	public TemplateModel getById(int id) {
		return dao.getById(id);
	}

	public ResultUtil save(TemplateModel model) {
		model.setTime(System.currentTimeMillis());
		if(model.getId()==0){
			return ResultUtil.init(dao.add(model)>0);
		}
		return ResultUtil.init(dao.update(model)>0);
	}

	public ResultUtil delete(List<Integer> ids) {
		try{
			return ResultUtil.init(dao.delete(ids)>0);
		}catch(Exception e){
			return ResultUtil.init(e);
		}
	}

	public List<TemplateModel> getAll() {
		return dao.getAll();
	}
}
